<template>
    <div>
        <el-card>
            <div class="info"><span>ID： </span>{{ this.UserInfo.aid }}</div>
            <div class="info"><span>用户名： </span>{{ this.UserInfo.adminname }}</div>
            <div class="info"><span>昵称： </span>{{ this.UserInfo.name }}</div>
            <div class="info"><span>性别： </span>{{ this.UserInfo.gender }}</div>
            <div class="info"><span>电话： </span>{{ this.UserInfo.tel }}</div>

            <div class="info"><span>权限： </span>{{ this.UserInfo.role }}</div>
            <el-button type="primary" @click="dialogFormVisible=true" >修改密码</el-button>

        </el-card>

        <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
            <el-form ref="passwordForm" :model="passwordForm" :rules="passwordFormRules">
                <el-form-item label="旧密码" :label-width="formLabelWidth" prop="last_password">
                    <el-input type="password" v-model="passwordForm.last_password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="新密码" :label-width="formLabelWidth" prop="new_password">
                    <el-input type="password" v-model="passwordForm.new_password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="重复新密码" :label-width="formLabelWidth" prop="new_password2">
                    <el-input type="password" v-model="passwordForm.new_password2" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="ok">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                dialogFormVisible:false,
                passwordForm:{},
                passwordFormRules: {
                    last_password: [
                        { required: true, message: '请输入旧密码', trigger: 'blur' }
                    ],

                    new_password: [
                        { required: true, message: '请输入新密码', trigger: 'blur' }
                    ],

                    new_password2: [
                        { required: true, message: '请重复新密码', trigger: 'blur' }
                    ],
                },
                formLabelWidth: '120px',
                UserInfo: {
                    aid: null,
                    adminname: '',
                    name: '',
                    gender: '',
                    tel: '',
                    role: ''
                }
            }
        },

        methods: {
            edit() {

            },
            cancel() {
                this.dialogFormVisible = false;
                this.passwordForm={username: null,
                    last_password: '',
                    new_password: '',
                    new_password2: ''}
            },
            ok(){
                let id = window.sessionStorage.getItem("id")
                console.log(id)
                if (this.passwordForm.new_password === this.passwordForm.new_password2){
                    this.$axios({
                        method:"post",
                        url:"http://localhost:80/admin/changePassword",
                        params:{
                            uid : this.UserInfo.aid,
                            oldpassword: this.passwordForm.last_password,
                            newpassword: this.passwordForm.new_password
                        }
                    }).then(res=>{
                        let msg
                        console.log(res)
                        if (res.data.msg === "success"){
                            this.$message.success("修改成功！")
                            this.passwordForm={username: null,
                                last_password: '',
                                new_password: '',
                                new_password2: ''}
                        }
                        else {
                            msg = res.data.msg
                            this.$message.error(msg)
                        }


                    })
                }else {
                    this.$message.error("新密码输入不一致")
                }
            },

        },

        async created() {
            this.UserInfo.adminname = window.sessionStorage.getItem("username")
            this.$axios({
                method:"post",
                url:"http://localhost:80/admin/getAdminByAccount",
                params:{
                    name:this.UserInfo.adminname}
            }).then(res =>{this.UserInfo = res.data.data
                this.UserInfo.u_id = this.UserInfo.uid
                console.log(this.UserInfo)}
            )
        }
        // console.log(res)
    };
</script>

<style lang="less" scoped>
  .info {
    margin-top: 25px;
  }
</style>
